#ifndef _GETSYM_H_
#define _GETSYM_H_

#include <iostream>
#include <string.h>
#include <fstream>
#include <queue>

using namespace std;

//单词类型
enum symtype
{
	SYM_NULL,//空
	SYM_IDENTIFIER,//关键字
	SYM_NUMBER,//
  SYS_ASG, //=
	SYM_PLUS,// +
	SYM_MINUS,// -
	SYM_TIMES,// *
	SYM_SLASH,//  /
	SYM_ODD,//   odd
	SYM_EQU,//==
	SYM_NEQ,// ！=
	SYM_LES,// <
	SYM_LEQ,//  <=
	SYM_GTR,//  >
	SYM_GEQ,// >=
	SYM_LPAREN,// (
	SYM_RPAREN,// )
	SYM_COMMA,//  ,
	SYM_SEMICOLON,//  ;
	SYM_PERIOD,// .
	SYM_BECOMES,// :=
  SYM_BEGIN,//  begin
	SYM_END,//  end
	SYM_IF,//  if
	SYM_THEN,//  then
	SYM_WHILE,//  while
	SYM_DO,//  do
	SYM_CALL,//  call
	SYM_CONST,//  const
	SYM_VAR,//  var
	SYM_PROCEDURE,//  procedure
  SYM_READ, //read
  SYM_WRITE, //write
	SYM_OVER // #
};


//字符串keyword_table[i]对应的类型为type_table[i]
static symtype type_table[31] = {SYS_ASG,SYM_PLUS,SYM_MINUS,SYM_TIMES,SYM_SLASH,SYM_ODD,SYM_EQU,SYM_NEQ,SYM_LES,SYM_LEQ,
SYM_GTR,SYM_GEQ,SYM_LPAREN,SYM_RPAREN,SYM_COMMA,SYM_SEMICOLON,SYM_PERIOD,SYM_BECOMES,SYM_BEGIN,SYM_END,SYM_IF,
SYM_THEN,SYM_WHILE,SYM_DO,SYM_CALL,SYM_CONST,SYM_VAR,SYM_PROCEDURE,SYM_READ,SYM_WRITE,SYM_OVER
};

static string keyword_table[31] = {"=","+","-","*","/","odd","==","<>","<","<=",">",">=","(",")",",",";",".",":=","begin","end",
"if","then","while","do","call","const","var","procedure","read","write","#"};

static string temp[34] = {
	"SYM_NULL","SYM_IDENTIFIER","SYM_NUMBER","SYS_ASG","SYM_PLUS","SYM_MINUS","SYM_TIMES",
	"SYM_SLASH","SYM_ODD","SYM_EQU","SYM_NEQ","SYM_LES","SYM_LEQ","SYM_GTR","SYM_GEQ","SYM_LPAREN",
	"SYM_RPAREN","SYM_COMMA","SYM_SEMICOLON","SYM_PERIOD","SYM_BECOMES","SYM_BEGIN","SYM_END",
	"SYM_IF","SYM_THEN","SYM_WHILE","SYM_DO","SYM_CALL","SYM_CONST","SYM_VAR","SYM_PROCEDURE","SYM_READ",
  "SYM_WRITE","SYM_OVER"
};

//词法分析器返回的一个单词结构
struct node {
  symtype type;  //单词类型
  string id_name;  //如果单词类型为标识符，id_name存储标识符本身
  int num;  //如果单词是无符号整数的话，存储整数的值

  node() {};
  node(symtype t) { type = t; };
  node(string name) { type = SYM_IDENTIFIER; id_name = name; };
  node(int n) { type = SYM_NUMBER; num = n; };
	node(symtype t,string name) { type = t; id_name = name; };//为非终结符准备

  friend ostream & operator << (ostream & out , node & n) {
    if(n.type==SYM_IDENTIFIER) {
      printf("标识符：%s\n", n.id_name.c_str());
    } else if(n.type==SYM_NUMBER) {
      printf("数字：%d\n", n.num);
    } else {
      printf("关键字：%s\n",temp[(int)(n.type)].c_str());
    }
  }
};

//词法分析器结构体
struct analyzer {
  ifstream code; //源文件输入流
  queue<node> node_queue; //已读取的单词结构的缓存队列

  analyzer(string filename);
  node get_a_node();
};


#endif
